#include "CommonUiManager.h"
#include "LayerManager.h"
#include "common/PIOStreamOperator.h"
#include "cocostudio/CocoStudio.h"
#include "CocosUiHelper.h"
#include "ui/UIText.h"

CCommonUiManager::CCommonUiManager(void)
{
	int32_t tBattleAnimals = GroupCount << 2;
	for (int32_t i = 0; i != tBattleAnimals; ++i)
		m_BattleAnimal.push_back(nullptr);

	m_ActionRange.resize(NQIPropertyBroadcastCount);
	m_ActionRange[NQIPropertyBroadcastReward] = ActionFrameRange(0, 10);
	m_ActionRange[NQIPropertyBroadcastBattle] = ActionFrameRange(11, 30);
}


CCommonUiManager::~CCommonUiManager(void)
{
}

cocos2d::Node* CCommonUiManager::GetCommonNode(int32_t index) {
	MapNode_t::const_iterator tIter = m_Nodes.find(index);
	if (tIter != m_Nodes.end())
		return tIter->second;

	cocos2d::Node* tNode = nullptr;
	switch (index) {
	case CGroupIndex:
		{
			tNode = CLayerManager::GetInstance()->GetLayer(CLayerManager::LayerIGroup);
			tNode->retain();
		}
		break;
	default:
		break;
	}

	if (nullptr != tNode)
		m_Nodes[index] = tNode;

	return tNode;
}

void CCommonUiManager::RemCommonNode(int32_t index) {
	MapNode_t::iterator tIter = m_Nodes.find(index);
	if (tIter != m_Nodes.end()) {
		cocos2d::Node* tNode = tIter->second;
		tNode->release();
		m_Nodes.erase(tIter);
	}
}

cocos2d::ui::ListView* CCommonUiManager::InitSkillView(cocos2d::Node* skillNode, SkillInfo* skill, int32_t start, int32_t end, CCommonUiUtil::VecNode_t& nodes) {
	cocos2d::ui::ListView* tSkillView = (cocos2d::ui::ListView*)CCocosUiHelper::seekNodeByTag(skillNode, 60);
	tSkillView->addEventListener((cocos2d::ui::ListView::ccListViewCallback)CC_CALLBACK_2(CCommonUiManager::onSkillSelect, this));

	int32_t tIndex = 0;
	for (int32_t i = start; i != end; ++i, ++tIndex) {
		tSkillView->pushBackCustomItem(CCommonUiUtil::initItemSkill(nodes, tIndex, skill));
		skill++;
	}

	return tSkillView;
}

void CCommonUiManager::RunPropertyBroadcast(const RunProppertyBroadcastParam& param) {
	if (m_CurParam.runNode != param.runNode) {
		for (VecNode_t::iterator it = m_PropertyBroadcast.begin(); it != m_PropertyBroadcast.end(); ++it) {
			cocos2d::Node* node = *it;
			node->setVisible(false);
		}
		m_CurParam = param;
	}
	cocos2d::Node* tNode = nullptr;
	for (VecNode_t::iterator it = m_PropertyBroadcast.begin(); it != m_PropertyBroadcast.end(); ++it) {
		cocos2d::Node* node = *it;
		if (!node->isVisible()) {
			node->setVisible(true);
			tNode = node;
		}
	}
	cocostudio::timeline::ActionTimeline* tAction = nullptr;
	if (nullptr == tNode) {
		tNode = cocos2d::CSLoader::createNode("Property_Broadcast.csb");
		tAction = cocos2d::CSLoader::createTimeline("Property_Broadcast.csb");
		
		tNode->retain();
		tNode->setUserData(tAction);
		param.runNode->addChild(tNode);
		tAction->setLastFrameCallFunc(std::bind([](cocos2d::Node* node)->void{ node->setVisible(false); node->removeFromParent(); }, tNode));
	}

	cocos2d::ui::Text* tName = (cocos2d::ui::Text*)CCocosUiHelper::seekNodeByTag(tNode, 877);
	tName->setString(param.name);
	cocos2d::ui::Text* tFlag = (cocos2d::ui::Text*)CCocosUiHelper::seekNodeByTag(tNode, 878);
	tFlag->setString(param.flag);
	cocos2d::ui::Text* tValue = (cocos2d::ui::Text*)CCocosUiHelper::seekNodeByTag(tNode, 879);
	std::string tStr;
	tStr << param.value;
	tValue->setString(tStr);

	tNode->runAction(tAction);
	tNode->setPosition(param.startPos);
	tAction->gotoFrameAndPlay(0, 10, true);
}

void CCommonUiManager::RunPropertyBroadcast(const RunProppertyBroadcastParam& param, NodeQueueIndex type) {
	cocos2d::Node* tNode = getCacheNode(type);
	param.runNode->addChild(tNode);
	cocostudio::timeline::ActionTimeline* tAction = (cocostudio::timeline::ActionTimeline*)tNode->getUserData();

	cocos2d::ui::Text* tName = (cocos2d::ui::Text*)CCocosUiHelper::seekNodeByTag(tNode, 877);
	tName->setString(param.name);
	cocos2d::ui::Text* tFlag = (cocos2d::ui::Text*)CCocosUiHelper::seekNodeByTag(tNode, 878);
	tFlag->setString(param.flag);
	cocos2d::ui::Text* tValue = (cocos2d::ui::Text*)CCocosUiHelper::seekNodeByTag(tNode, 879);
	std::string tStr;
	tStr << param.value;
	tValue->setString(tStr);

	tNode->setVisible(true);
	tNode->runAction(tAction);
	tNode->setPosition(param.startPos);
	tAction->gotoFrameAndPlay(m_ActionRange[type].start, m_ActionRange[type].end, false);
}

cocos2d::Node* CCommonUiManager::GetBattleAnimal(const BattleAnimalParam& param) {
	if (param.index >= GroupCount) {
		assert(false);
	}
	int32_t tIndex = param.index;
	if (param.isUp) {
		tIndex += (GroupCount << 1);
		if (param.isUser)
			tIndex += GroupCount;
	} else {
		if (!param.isUser)
			tIndex += GroupCount;
	}

	cocos2d::Node* tNode = m_BattleAnimal[tIndex];
	if (nullptr == tNode) {
		tNode = cocos2d::CSLoader::createNode("Battle_Animal.csb");
		tNode->retain();
		m_BattleAnimal[tIndex] = tNode;
	}
	return tNode;
}

cocos2d::Node* CCommonUiManager::getCacheNode(NodeQueueIndex index) {
	cocos2d::Node* tNode = nullptr;
	QueNode_t* tQue = nullptr;

	MapQueNode_t::iterator tIter = m_QueNode.find(index);
	if (tIter != m_QueNode.end()) {
		tQue = &tIter->second;
	} else {
		QueNode_t tQue1;
		m_QueNode[index] = tQue1;
		tQue = &m_QueNode[index];
	}

	if (tQue->empty())
		tNode = initNode(index);
	else {
		tNode = tQue->front();
		tQue->pop();
	}

	return tNode;
}

void CCommonUiManager::setCacheNode(NodeQueueIndex index, cocos2d::Node* node) {
	MapQueNode_t::iterator tIter = m_QueNode.find(index);
	if (tIter != m_QueNode.end()) {
		QueNode_t& tQue = tIter->second;
		tQue.push(node);
	} else {
		assert(false);
	}
}

cocos2d::Node* CCommonUiManager::initNode(NodeQueueIndex index) {
	cocos2d::Node* tNode = nullptr;
	switch (index)
	{
	case CCommonUiManager::NQIPropertyBroadcastReward:
	case CCommonUiManager::NQIPropertyBroadcastBattle:
		{
			cocostudio::timeline::ActionTimeline* tAction = nullptr;
			tNode = cocos2d::CSLoader::createNode("Property_Broadcast.csb");
			tAction = cocos2d::CSLoader::createTimeline("Property_Broadcast.csb");

			tNode->retain();
			tAction->retain();
			tNode->setUserData(tAction);
			tAction->setLastFrameCallFunc(std::bind([&](cocos2d::Node* node, NodeQueueIndex index)->void{ node->setVisible(false); node->removeFromParent(); setCacheNode(index, node); }, tNode, index));
		} break;
	default:
		break;
	}

	return tNode;
}

void CCommonUiManager::onSkillSelect(cocos2d::Ref* sender, cocos2d::ui::ListView::EventType type) {
	switch (type)
	{
	case cocos2d::ui::ListView::EventType::ON_SELECTED_ITEM_END:
		{
			cocos2d::ui::ListView* tListView = (cocos2d::ui::ListView*)(sender);
			auto tSelectItem = tListView->getItem(tListView->getCurSelectedIndex());

			Vector<cocos2d::ui::Widget*>& tItems = tListView->getItems();
			for (Vector<cocos2d::ui::Widget*>::const_iterator it = tItems.begin(); it != tItems.end(); ++it) {
				cocos2d::ui::Widget* tItem = *it;
				if (tItem == tSelectItem)
					tItem->setOpacity(30);
				else
					tItem->setOpacity(255);
			}
		}
		break;
	default:
		break;
	}
}
